package com.example.wwj.service;

import com.example.wwj.mapper.ClassNameMapper;
import com.example.wwj.mapper.GradeNameMapper;
import com.example.wwj.mapper.StudentMapper;
import com.example.wwj.pojo.*;
import com.example.wwj.vo.ResultVo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class StudentService {

    @Autowired
    private GradeNameMapper gradeNameMapper;
    @Autowired
    private ClassNameMapper classNameMapper;
    @Autowired
    private StudentMapper studentMapper;


    public List<Student> findAll(){
        return studentMapper.findAll();
    }

    public List<ClassName> classNameList() {return classNameMapper.classList();}

    public List<GradeName> gradeNameList() {return gradeNameMapper.gradeList();}

    public ClassName findClassNameById(int class_id){
        return classNameMapper.findClassNameById(class_id);
    }

    public GradeName findGradeNameById(int grade_id){
        return gradeNameMapper.findGradeNameById(grade_id);
    }

    public ResultVo save(Student student){
        // 根据 class_id 查询班级名称
        ClassName className = classNameMapper.findClassNameById(student.getClass_id());
        // 根据 grade_id 查询年级名称
        GradeName gradeName = gradeNameMapper.findGradeNameById(student.getGrade_id());

        // 将查询到的className和gradeName作为新增数据进行新增操作
        student.setClass_name(className.getClass_name());
        student.setGrade_name(gradeName.getGrade_name());

        studentMapper.insert(student);
        // 返回成功的结果
        return ResultVo.successVo();
    }

    public ResultVo update(Student student){
        // 根据 class_id 查询班级名称
        ClassName className = classNameMapper.findClassNameById(student.getClass_id());
        // 根据 grade_id 查询年级名称
        GradeName gradeName = gradeNameMapper.findGradeNameById(student.getGrade_id());
        // 将查询到的className和gradeName作为新增数据进行新增操作
        student.setClass_name(className.getClass_name());
        student.setGrade_name(gradeName.getGrade_name());
         studentMapper.updateStudent(student);
         return ResultVo.successVo();
    }

    public List<Student> selectPage(Integer pageNum, Integer pageSize,Integer class_id,Integer grade_id,String name) {
        return studentMapper.selectPage(pageNum,pageSize,class_id,grade_id,name);
    }

    public Integer selectTotal(Integer class_id,Integer grade_id,String name) {
        return studentMapper.selectTotal(class_id,grade_id,name);
    }

    public int deleteById(List<Integer> ids){
        System.out.println(ids);
        Map<String, Object> map = new HashMap<>();
        map.put("ids", ids);
       return studentMapper.deleteStudentsByIds(map);
    }


}
